﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using NHunspell;
using System.Text.RegularExpressions;

namespace OfficeClip.OpenSource.SpellO
{
	public class Speller
	{
        //private int minimumWordLength = 3;
        //private List<string> allowedWords = new List<string> { 
        //    "OfficeClip", 
        //    "Adobe", 
        //    "Blog" ,
        //    "Extranet",
        //    "http",
        //    "csv",
        //    "www"
        //};
		private Settings.SpellO _spello;
        private int _siteIndex;

		public Hashtable GetRecommendations(string text, Settings.SpellO spello, int siteIndex)
		{
			_spello = spello;
            _siteIndex = siteIndex;
			Hashtable hRecommend = new Hashtable();
            // SK - null value of text is skipped
            if (!string.IsNullOrEmpty(text))
            {
                //Blag - To split non-braking spaces, I have replaced them with normal spaces
                text = Regex.Replace(text, @"\u00A0", " ");
                List<string> words = SplitWords(text);
                List<string> suggestions;
                string word = string.Empty;
                string dictPrefix = Constants.GetSetupDir() + @"\";
                if (words.Count > 0)
                {
                    using (Hunspell hunspell = new Hunspell(
                        dictPrefix + _spello.Sites[_siteIndex].SpellCheck.Dictionary + ".aff",
                        dictPrefix + _spello.Sites[_siteIndex].SpellCheck.Dictionary + ".dic"
                        ))
                    {
                        foreach (string wordStr in words)
                        {
                            word = wordStr.Trim();
                            if (!hunspell.Spell(word))
                            {
                                suggestions = new List<string>();
                                foreach (string suggest in hunspell.Suggest(word))
                                {
                                    suggestions.Add(((string) suggest).Trim());
                                }
                                hRecommend.Add(word, suggestions);
                            }
                        }
                    }
                }
            }
		    return hRecommend;
		}

		private List<string> SplitWords(string text)
		{
			// First split the text into words
			List<string> words = new List<string>();
			string delimiter = @" ,.:;~!@#$%^&*(){}\/[]<>|?؟-_+'،""="; //added ' to delimiters
			delimiter += "\r\n\t";
			string[] sarr = null;
			if (!string.IsNullOrEmpty(text))
			{
				sarr = text.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
				// now lets sort the array and remove duplicates
				foreach (string str in sarr)
				{
					if (
						(!words.Contains(str)) &&
						(str.Length >= _spello.Sites[_siteIndex].SpellCheck.MinimumWordLength) &&
						(
                        !_spello.Sites[_siteIndex].SpellCheck.IgnoreAllCapitals |
						(string.Compare(str.ToUpper(), str, false) != 0)
						)&&
                        (!_spello.Sites[_siteIndex].SpellCheck.AllowWords.Contains(str))
						)
					{
						words.Add(str);
					}
				}
			}
			return words;
		}

	}
}
